﻿using MengCC.Core.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MengCC.Core.Projects
{
    public class ProjectManager
    {
        private IBaseRepository<BuildLog, string> buildLogRepository;
        private IBaseRepository<Project, int> projectRepository;

        public ProjectManager(IBaseRepository<BuildLog, string> buildLogRepository, IBaseRepository<Project, int> projectRepository)
        {
            this.buildLogRepository = buildLogRepository;
            this.projectRepository = projectRepository;
        }

        public BuildLog AddBuildLog(Project project)
        {
            return AddBuildLog(new BuildLog
            {
                ProjectId = project.Id,
                ProjectName = project.Name,
                BuildTime = DateTime.Now,
                BuildState = BuildLog.BuildStateEnum.Building
            });
        }


        /// <summary>
        /// 添加构建日志
        /// </summary>
        /// <param name="buildLog"></param>
        public BuildLog AddBuildLog(BuildLog buildLog)
        {
            var result = buildLogRepository.GetPaggingList(1,1,where:l=>l.ProjectId==buildLog.ProjectId,orderby:l=>l.ModifiedTime);
            if (result == null|| result.Total==0)
                buildLog.BuildNum = 1;
            else
                buildLog.BuildNum = result.Items.FirstOrDefault().BuildNum+ 1;

            buildLog.Id = $"{buildLog.ProjectId}-{buildLog.BuildNum}";
            buildLogRepository.Add(buildLog);
            return buildLog;
        }
        /// <summary>
        /// 更新构建日志
        /// </summary>
        /// <param name="buildLog"></param>
        /// <returns></returns>
        public BuildLog UpdateBuildLog(BuildLog buildLog)
        {
            buildLogRepository.Update(buildLog);
            return buildLog;
        }
        /// <summary>
        /// 移除构建日志
        /// </summary>
        /// <param name="logids"></param>
        public void RemoveBuildLog(params string[] logids)
        {
            foreach (var item in logids)
            {
                buildLogRepository.Delete(item);
            }
        }
        /// <summary>
        /// 获取构建历史列表
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public PaggingEntity<BuildLog> GetBuildLogs(int projectId,int pageindex,int pagesize=10)
        {
            return buildLogRepository.GetPaggingList(pageindex, pagesize,null, l => l.ProjectId == projectId, l => l.ModifiedTime, false);
        }
        /// <summary>
        /// 获取构建记录
        /// </summary>
        /// <param name="buildlogid"></param>
        /// <returns></returns>
        public BuildLog GetBuildLog(string buildlogid)
        {
            return buildLogRepository.Get(buildlogid);
        }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="project"></param>
        public Project AddProject(Project project)
        {
            return projectRepository.Add(project);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public bool IsNameAndCodeAvailable (Project project)
        {
            return projectRepository.GetList(p => (p.Name == project.Name || p.Code == project.Code)&&p.Id!=project.Id).Count() == 0;
        }
        /// <summary>
        /// 获取项目
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public Project GetProject(int projectid)
        {
            return projectRepository.Get(projectid);
        }
        /// <summary>
        /// 根据 code 和 token 获取项目
        /// </summary>
        /// <param name="code"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public Project GetProject(string code,string token)
        {
            return projectRepository.Get(p=>p.Code==code&&p.RemoteToken==token);
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="projectids"></param>
        public void RemoveProject(params int[] projectids)
        {
            foreach (var projectid in projectids)
            {
                projectRepository.Delete(projectid);
                RemoveBuildLog(buildLogRepository.GetList(b => b.ProjectId == projectid).Select(b => b.Id).ToArray());
            }
        }
        /// <summary>
        /// 更新项目
        /// </summary>
        /// <param name="project"></param>
        public void UpdateProject(Project project)
        {
            projectRepository.Update(project);
        }
        /// <summary>
        /// 分页项目列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IEnumerable<Project> GetProjects(int userid=0)
        {
            return projectRepository.GetList(p=> userid>0&&p.ModifiedUserId== userid ||true, l => l.Id, false);
        }
    }
}
